<!doctype html>



  


<html class="theme-next muse use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.1" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Zabbix," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.1" />






<meta name="description" content="&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;zabbix支持mail报警，sms报警，以及自定义报警，用到最多的还是脚本报警，当事件通知到脚本，会传递给脚本三个参数。  $1：发送给谁 $2：标题 $3：报警内容  &amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;添加媒介类型：Adminstrato">
<meta name="keywords" content="Zabbix">
<meta property="og:type" content="article">
<meta property="og:title" content="Zabbix脚本报警介质自定义">
<meta property="og:url" content="https://hcldirgit.github.io/2017/09/03/Zabbix/7. Zabbix脚本报警介质自定义/index.html">
<meta property="og:site_name" content="失落的乐章">
<meta property="og:description" content="&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;zabbix支持mail报警，sms报警，以及自定义报警，用到最多的还是脚本报警，当事件通知到脚本，会传递给脚本三个参数。  $1：发送给谁 $2：标题 $3：报警内容  &amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;添加媒介类型：Adminstrato">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/Zabbix%E8%84%9A%E6%9C%AC%E6%8A%A5%E8%AD%A6%E4%BB%8B%E8%B4%A8%E8%87%AA%E5%AE%9A%E4%B9%89/01.jpg?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/Zabbix%E8%84%9A%E6%9C%AC%E6%8A%A5%E8%AD%A6%E4%BB%8B%E8%B4%A8%E8%87%AA%E5%AE%9A%E4%B9%89/02.jpeg?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/Zabbix%E8%84%9A%E6%9C%AC%E6%8A%A5%E8%AD%A6%E4%BB%8B%E8%B4%A8%E8%87%AA%E5%AE%9A%E4%B9%89/03.jpeg?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/Zabbix%E8%84%9A%E6%9C%AC%E6%8A%A5%E8%AD%A6%E4%BB%8B%E8%B4%A8%E8%87%AA%E5%AE%9A%E4%B9%89/04.jpeg?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/Zabbix%E8%84%9A%E6%9C%AC%E6%8A%A5%E8%AD%A6%E4%BB%8B%E8%B4%A8%E8%87%AA%E5%AE%9A%E4%B9%89/05.jpeg?raw=true">
<meta property="og:updated_time" content="2017-09-02T17:38:04.312Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Zabbix脚本报警介质自定义">
<meta name="twitter:description" content="&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;zabbix支持mail报警，sms报警，以及自定义报警，用到最多的还是脚本报警，当事件通知到脚本，会传递给脚本三个参数。  $1：发送给谁 $2：标题 $3：报警内容  &amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;添加媒介类型：Adminstrato">
<meta name="twitter:image" content="https://github.com/hcldirgit/image/blob/master/Zabbix%E8%84%9A%E6%9C%AC%E6%8A%A5%E8%AD%A6%E4%BB%8B%E8%B4%A8%E8%87%AA%E5%AE%9A%E4%B9%89/01.jpg?raw=true">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://hcldirgit.github.io/2017/09/03/Zabbix/7. Zabbix脚本报警介质自定义/"/>





  <title>Zabbix脚本报警介质自定义 | 失落的乐章</title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  




<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
            (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
          m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
  ga('create', '85*****1', 'auto');
  ga('send', 'pageview');
</script>


  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?87980c**************99ec5e26fb5";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>











  
  
    
  

  <div class="container sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">失落的乐章</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">技术面前，永远都是学生。</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-message">
          <a href="/message" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-external-link"></i> <br />
            
            留言
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="https://hcldirgit.github.io/2017/09/03/Zabbix/7. Zabbix脚本报警介质自定义/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="失落的乐章">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/0.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="失落的乐章">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Zabbix脚本报警介质自定义</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-09-03T02:13:44+08:00">
                2017-09-03
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;zabbix支持mail报警，sms报警，以及自定义报警，用到最多的还是脚本报警，当事件通知到脚本，会传递给脚本三个参数。</p>
<ul>
<li>$1：发送给谁</li>
<li>$2：标题</li>
<li>$3：报警内容</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;添加媒介类型：Adminstrator→Media Type→Add</p>
<p><img src="https://github.com/hcldirgit/image/blob/master/Zabbix%E8%84%9A%E6%9C%AC%E6%8A%A5%E8%AD%A6%E4%BB%8B%E8%B4%A8%E8%87%AA%E5%AE%9A%E4%B9%89/01.jpg?raw=true" alt=""></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;那么上面提到的sms.sh放在哪里呢？</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">[root@aliyun-test ~]<span class="comment"># cd /usr/lib/zabbix/alertscripts/</span></div><div class="line">[root@aliyun-test alertscripts]<span class="comment"># vim sms.sh</span></div><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line">ALERT_TO=<span class="variable">$1</span></div><div class="line">ALERT_TITLE=<span class="variable">$2</span></div><div class="line">ALERT_BODY=<span class="variable">$3</span></div><div class="line"></div><div class="line"><span class="built_in">echo</span> <span class="variable">$ALERT_TO</span> &gt;&gt; /tmp/sms.log</div><div class="line"><span class="built_in">echo</span> <span class="variable">$ALERT_TITLE</span> &gt;&gt; /tmp/sms.log</div><div class="line"><span class="built_in">echo</span> <span class="variable">$ALERT_BODY</span> &gt;&gt; /tmp/sms.log</div><div class="line">[root@aliyun-test alertscripts]<span class="comment"># chmod +x ./sms.sh</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;上面这个目录在zabbix的server配置段有定义，定义内容如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="comment">### Option: AlertScriptsPath</span></div><div class="line"><span class="comment"># Full path to location of custom alert scripts.</span></div><div class="line"><span class="comment"># Default depends on compilation options.</span></div><div class="line"><span class="comment">#</span></div><div class="line"><span class="comment"># Mandatory: no</span></div><div class="line"><span class="comment"># Default:</span></div><div class="line"><span class="comment"># AlertScriptsPath=$&#123;datadir&#125;/zabbix/alertscripts</span></div><div class="line"></div><div class="line">AlertScriptsPath=/usr/lib/zabbix/alertscripts</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;添加Actions：</p>
<p><img src="https://github.com/hcldirgit/image/blob/master/Zabbix%E8%84%9A%E6%9C%AC%E6%8A%A5%E8%AD%A6%E4%BB%8B%E8%B4%A8%E8%87%AA%E5%AE%9A%E4%B9%89/02.jpeg?raw=true" alt=""></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改用户的媒体类型：</p>
<p><img src="https://github.com/hcldirgit/image/blob/master/Zabbix%E8%84%9A%E6%9C%AC%E6%8A%A5%E8%AD%A6%E4%BB%8B%E8%B4%A8%E8%87%AA%E5%AE%9A%E4%B9%89/03.jpeg?raw=true" alt=""></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;添加完了以后，还记得我们之前添加过得nginx.active&gt;10的触发器么。这里去触发一下触发器然后观察event事件。</p>
<p><img src="https://github.com/hcldirgit/image/blob/master/Zabbix%E8%84%9A%E6%9C%AC%E6%8A%A5%E8%AD%A6%E4%BB%8B%E8%B4%A8%E8%87%AA%E5%AE%9A%E4%B9%89/04.jpeg?raw=true" alt=""></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;我们发现他在发信息呢。看看我们定义的/tmp下的sms.log有没有内容呢：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">[root@aliyun-test ~]<span class="comment"># tail -F /tmp/sms.log </span></div><div class="line"><span class="built_in">test</span>@admin.com</div><div class="line">PROBLEM: 当nginx的动态连接数大于10的话我就报警</div><div class="line"> Original event ID: 454*:*UNKNOWN*): *UNKNOWN*1</div><div class="line"><span class="built_in">test</span>@admin.com</div><div class="line">PROBLEM: 当nginx的动态连接数大于10的话我就报警</div><div class="line"> Original event ID: 455*:*UNKNOWN*): *UNKNOWN*9</div><div class="line"><span class="built_in">test</span>@admin.com</div><div class="line">PROBLEM: 当nginx的动态连接数大于10的话我就报警</div><div class="line"> Original event ID: 456*:*UNKNOWN*): *UNKNOWN*4</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;可以发现确实写入到文本了。那么在实际的应用中我们就可以使用传过来的$1 $2 $3这三个来灵活的去处理了。只要获取到这个内容我们就可以按照自己的要求去处理了，不管你是发短信还是发邮件都是OK的。这取决于你的脚本到底是怎么写的。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;关于短信发送的相关信息我们可以参考<a href="https://dayu.aliyun.com/" target="_blank" rel="external">阿里大于</a>，在<a href="https://api.alidayu.com/doc2/apiDetail?apiId=25450" target="_blank" rel="external">阿里大于的帮助平台</a>提供有短信发送的API以及相关参数。当然也可以使用Curl来完成，不过就是稍微有点麻烦就是了。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;实践需要注意的一些内容：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Actions中定义的内容都是我们要收到的内容，如果你使用邮件这应该问题不大，不过你要是使用短信的话你就需要注意点了。因为在国内一条短信的长度是有限制的，搞不好一条短信发不完还得分开好几条发送，如果告警多了这个花销也是不可小看的。因此我们可以把actions改造一下，其实获取关键信息就足够了，定位机器，定位故障等。</p>
<p><img src="https://github.com/hcldirgit/image/blob/master/Zabbix%E8%84%9A%E6%9C%AC%E6%8A%A5%E8%AD%A6%E4%BB%8B%E8%B4%A8%E8%87%AA%E5%AE%9A%E4%B9%89/05.jpeg?raw=true" alt=""></p>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Zabbix/" rel="tag"># Zabbix</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/09/03/Zabbix/6. zabbix使用自动发现功能监控服务器各JVM进程状态/" rel="next" title="zabbix使用自动发现功能监控服务器各JVM进程状态">
                <i class="fa fa-chevron-left"></i> zabbix使用自动发现功能监控服务器各JVM进程状态
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/09/03/Zabbix/8. Zabbix事件通知/" rel="prev" title="Zabbix事件通知">
                Zabbix事件通知 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      

      <section class="site-overview sidebar-panel sidebar-panel-active">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/0.png"
               alt="失落的乐章" />
          <p class="site-author-name" itemprop="name">失落的乐章</p>
           
              <p class="site-description motion-element" itemprop="description">失落的乐章的Blog</p>
          
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/">
                <span class="site-state-item-count">627</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          

          
            
            
            <div class="site-state-item site-state-tags">
              <a href="/tags/index.html">
                <span class="site-state-item-count">38</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/hcldirgit" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
          
        </div>

        
        

        
        

        


      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">失落的乐章</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Muse
  </a>
</div>


        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.1"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.1"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.1"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.1"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.1"></script>



  


  




	





  





  





  






  





  

  

  

  

  

  

</body>
</html>
